package com.hxhk.web.controller.module.staff;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hxhk.common.springmvc.DateConvertEditor;
import com.hxhk.pojo.Criteria;
import com.hxhk.pojo.ExceptionReturn;
import com.hxhk.pojo.ExtGridReturn;
import com.hxhk.pojo.ExtPager;
import com.hxhk.pojo.ExtReturn;
import com.hxhk.pojo.module.staff.Staff;
import com.hxhk.service.module.staff.StaffService;

/**
 * 员工信息
 * 
 * @author wangl
 * @date 2014-2-10 下午12:47:37
 */
@Controller
@RequestMapping("/staff")
public class StaffController {
	/**
	 * 日志操作对象
	 */
	private static Logger logger = LoggerFactory.getLogger(StaffController.class);
	@Autowired
	private StaffService staffService;
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(Date.class, new DateConvertEditor("yyyy-MM-dd"));
		binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
	}
	
	/**
	 * index
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String staff() {
		return "module/staff/Staff";
	}
	
	/**
	 * 查找所有的用户
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public Object all(ExtPager pager, @RequestParam(required = false, defaultValue = "") String staffName) {
		Criteria criteria = new Criteria();
		// 设置分页信息
		if (pager.getLimit() != null && pager.getStart() != null) {
			criteria.setOracleEnd(pager.getLimit() + pager.getStart());
			criteria.setOracleStart(pager.getStart());
		}
		// 排序信息
		if (StringUtils.isNotBlank(pager.getDir()) && StringUtils.isNotBlank(pager.getSort())) {
			criteria.setOrderByClause(pager.getSort() + " " + pager.getDir());
		}
		if (StringUtils.isNotBlank(staffName)) {
			criteria.put("staffName", staffName);
		}
		List<Staff> list = this.staffService.selectByExample(criteria);
		int total = this.staffService.countByExample(criteria);
		//System.out.println(new ExtGridReturn(total, list).toString());
		return new ExtGridReturn(total, list);
	}
	
	/**
	 * 保存用户信息
	 */
	@RequestMapping("/save")
	@ResponseBody
	public Object save(Staff staff) {
		try {
			if (StringUtils.isBlank(staff.getStaffName())) {
				return new ExtReturn(false, "员工姓名不能为空！");
			}
			Criteria criteria = new Criteria();
			criteria.put("staff", staff);
			String result = this.staffService.saveStaff(criteria);
			if ("01".equals(result)) {
				return new ExtReturn(true, "保存成功！");
			} else if ("00".equals(result)) {
				return new ExtReturn(false, "保存失败！");
			} else {
				return new ExtReturn(false, result);
			}
		} catch (Exception e) {
			logger.error("Exception: ", e);
			return new ExceptionReturn(e);
		}
	}
	/**
	 * 删除该用户
	 */
	@RequestMapping("/del/{staffId}")
	@ResponseBody
	public Object delete(@PathVariable String staffId) {
		try {
			if (StringUtils.isBlank(staffId)) {
				return new ExtReturn(false, "用户主键不能为空！");
			}
			Criteria criteria = new Criteria();
			criteria.put("staffId", staffId);
			String result = this.staffService.deleteByPrimaryKey(criteria);
			if ("01".equals(result)) {
				return new ExtReturn(true, "删除成功！");
			} else if ("00".equals(result)) {
				return new ExtReturn(false, "删除失败！");
			} else {
				return new ExtReturn(false, result);
			}
		} catch (Exception e) {
			logger.error("Exception: ", e);
			return new ExceptionReturn(e);
		}
	}
}
